//
//  ZJRecFirstStoryCollectionViewCell.m
//  BreadTravel
//
//  Created by 张健 on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJRecFirstStoryCollectionViewCell.h"

@implementation ZJRecFirstStoryCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubview];
    }
    return self;
}

- (void)createSubview{
    
    //底色
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 10;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    //上面的大图片
    self.headImageview = [[UIImageView alloc]init];
    self.headImageview.layer.masksToBounds = YES;
    self.headImageview.layer.cornerRadius = 10;
    self.headImageview.backgroundColor = [UIColor redColor];
    [bgView addSubview:self.headImageview];
    [self.headImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.mas_equalTo(0);
        make.height.mas_equalTo(115);
        make.width.equalTo(bgView);
    }];
    
    //下面白色的背景
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headImageview.mas_bottom).offset(-10);
        make.left.equalTo(bgView);
        make.width.equalTo(bgView);
        make.height.mas_equalTo(90);
    }];
    
    //图片上面字
    self.locationLabel = [[UILabel alloc]init];
    self.locationLabel.font = [UIFont systemFontOfSize:12];
    [self.headImageview addSubview:self.locationLabel];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.headImageview).offset(-20);
        make.left.equalTo(self.headImageview).offset(10);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(100);
    }];
    
    //标题文字
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(bottomView.mas_top).offset(10);
    }];
    
    //头像
    self.avatarImageView = [[UIImageView alloc]init];
    self.avatarImageView.backgroundColor = [UIColor grayColor];
    self.avatarImageView.layer.masksToBounds = YES;
    self.avatarImageView.layer.cornerRadius = 12.5;
    [bottomView addSubview:self.avatarImageView];
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.bottom.equalTo(bottomView.mas_bottom).offset(-15);
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(25);
    }];
    
    //名字
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    [bottomView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.avatarImageView.mas_right).offset(10);
        make.right.mas_equalTo(-10);
        make.bottom.equalTo(bottomView.mas_bottom).offset(-20);
    }];
    
    
}

-(void)setTableModel:(ZJTableModel *)tableModel{
    

    ZJUserModel *userModel = tableModel.userModel;
    
    NSURL *url1 = [NSURL URLWithString:tableModel.index_cover];
    [self.headImageview sd_setImageWithURL:url1 placeholderImage:IMAGE];
    
    NSURL *url2 = [NSURL URLWithString:userModel.avatar_m];
    [self.avatarImageView sd_setImageWithURL:url2 placeholderImage:IMAGE];
    
    self.nameLabel.text = userModel.name;
    self.titleLabel.text = tableModel.index_title;
    self.locationLabel.text = tableModel.location_alias;
    if ([tableModel.index_title isEqualToString:@""]) {
        self.titleLabel.text = tableModel.text;
    }
}

@end
